﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using RenrenApi;

namespace Web
{
    public partial class search : System.Web.UI.Page
    {
        private const int pageSize = 21;
        protected void Page_Load(object sender, EventArgs e)
        {
            var _type = Request["type"];
            var _value = Request["value"];

            if(_value != null) value.Value = _value;
            if(_type != null) type.Value = _type;
            if (current_page.Value == "") current_page.Value = "1";
            if ( sort_type.Value == "") sort_type.Value = "0";

            fillCourse();
            fillPage();
            fillLiteral();
            if (current_page.Value == "1") page_up.Enabled = false; else page_up.Enabled = true;
            if (current_page.Value == page_count.Value) page_down.Enabled = false; else page_down.Enabled = true;

        }
        protected void fillPage()
        {
            Repeater_Page.DataSource = new int[int.Parse(page_count.Value)];
            Repeater_Page.DataBind();
        }
        protected void fillCourse()
        {
            int _page_count;
            int _course_num;
            Repeater_Content.DataSource = new SearchBll().GetCourses(type.Value, value.Value, int.Parse(sort_type.Value), int.Parse(current_page.Value), pageSize, out _page_count, out _course_num);
            Repeater_Content.DataBind();
            page_count.Value = _page_count.ToString();
            course_num.Value = _course_num.ToString();

        }
        protected void fillLiteral ()
        {
            switch (sort_type.Value)
            {
                case "0":
                    Literal0.Text = "人气";
                    Literal1.Text = "含金量";
                    Literal2.Text = "轻松度";
                    Literal3.Text = "成绩值";
                    break;
                case "1":
                    Literal0.Text = "含金量";
                    Literal1.Text = "人气";
                    Literal2.Text = "轻松度";
                    Literal3.Text = "成绩值";
                    break;
                case "2":
                    Literal0.Text = "轻松度";
                    Literal1.Text = "含金量";
                    Literal2.Text = "人气";
                    Literal3.Text = "成绩值";
                    break;
                case "3":
                    Literal0.Text = "成绩值";
                    Literal1.Text = "含金量";
                    Literal2.Text = "轻松度";
                    Literal3.Text = "人气";
                    break;
            }
            LiteralNum.Text = course_num.Value;
            switch (type.Value)
            {
                case "1":
                case "2":
                    LiteralName1.Text = value.Value;
                    LiteralName2.Text = value.Value;
                    break;
                case "3":
                    LiteralName1.Text = "英语课";
                    LiteralName2.Text = "英语课";
                    break;
                case "4":
                    LiteralName1.Text = "体育课";
                    LiteralName2.Text = "体育课";
                    break;
                case "5":
                    LiteralName1.Text = "第二外国语";
                    LiteralName2.Text = "第二外国语";
                    break;
                case "6":
                    LiteralName1.Text = "推荐最热门";
                    LiteralName2.Text = "推荐最热门";
                    break;
                case "7":
                    LiteralName1.Text = "内容最精华";
                    LiteralName2.Text = "内容最精华";
                    break;
                case "8":
                    LiteralName1.Text = "给分最厚道";
                    LiteralName2.Text = "给分最厚道";
                    break;
                case "10":
                    LiteralName1.Text = "搜索结果";
                    LiteralName2.Text = "搜索结果";
                    break;
                case "11":
                    LiteralName1.Text = "同名课程";
                    LiteralName2.Text = "同名课程";
                    break;
            }
        }
        // 搜索按钮
        protected void search_button_Click(object sender, EventArgs e)
        {
            Response.Redirect("search.aspx?type=10&value=" + search_input.Text);
        }
        protected void sortType_button_Click(object sender, EventArgs e)
        {
            var btn = sender as LinkButton;

            switch (btn.ID)
            {
                case "LinkButton1":
                    sort_type.Value = findSort(Literal1.Text).ToString();
                    break;
                case "LinkButton2":
                    sort_type.Value = findSort(Literal2.Text).ToString();
                    break;
                case "LinkButton3":
                    sort_type.Value = findSort(Literal3.Text).ToString();
                    break;
            }
            fillCourse();
            fillPage();
            fillLiteral();
        }
        private void PageClick(object sender, EventArgs e)
        {
            int page = int.Parse(((LinkButton)sender).ID.Substring(4));
            current_page.Value = page.ToString();
            if (current_page.Value == "1") page_up.Enabled = false; else page_up.Enabled = true;
            if (current_page.Value == page_count.Value) page_down.Enabled = false; else page_down.Enabled = true;
            fillCourse();
            fillPage();
        }
        protected void PlaceClick(object sender,EventArgs e)
        {
            var button = sender as LinkButton;
            string courseid = button.ID.ToString();
            Response.Redirect("course.aspx?id="+courseid);
        }
        protected void pageUpClick(object sender,EventArgs e)
        {
            current_page.Value = (int.Parse(current_page.Value) - 1).ToString();
            if (current_page.Value == "1") page_up.Enabled = false; else page_up.Enabled = true;
            if (current_page.Value == page_count.Value) page_down.Enabled = false; else page_down.Enabled = true;
            fillCourse();
            fillPage();
        }
        protected void pageDownClick(object sender,EventArgs e)
        {
            current_page.Value = (int.Parse(current_page.Value) + 1).ToString();
            if (current_page.Value == "1") page_up.Enabled = false; else page_up.Enabled = true;
            if (current_page.Value == page_count.Value) page_down.Enabled = false; else page_down.Enabled = true;
            fillCourse();
            fillPage();
        }
        
        private int findSort(string s)
        {
            switch (s)
            {
                case "人气":
                    return 0;
                    break;
                case "含金量":
                    return 1;
                    break;
                case "轻松度":
                    return 2;
                    break;
                case "成绩值":
                    return 3;
                    break;
            }
            return 0;
        } 

        protected void DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            int index = e.Item.ItemIndex;
            Course course = (Course)e.Item.DataItem;
            switch (sort_type.Value)
            {
                case "3":
                case "2":
                case "1":
                case "0": //
                    StringBuilder prefix= new StringBuilder(); 
                    StringBuilder suffix = new StringBuilder();
                    if(type.Value.Equals("6"))//推荐最热门
                    {
                        if (index % 3 == 2)
                        {
                            /*     prefix.Append("<li style=\"margin-right: 0;\"><a href=\"" + "course.aspx?id=" + course.ID + "\" class=\"course\" title=" + course.CourseName + ">" +
                                           Utility.ShortenClassName(course.CourseName) + "</a>");
                                 prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.TeacherID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                           Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                                 prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");

                                 suffix.Append(
                                     "<div class=\"bar_list\"><div class=\"plain_text\"><span class=\"bar_label\">含金量</span> <span class=\"number\">" +
                                     Utility.Round(course.CourseInfo.Gold) + "</span></div>");
                                 suffix.Append(
                                     "<div class=\"plain_text\"><span class=\"bar_label\">轻松度</span> <span class=\"number\">" +
                                     Utility.Round(course.CourseInfo.Relax) + "</span></div>");
                                 suffix.Append(
                                     "<div class=\"plain_text\"><span class=\"bar_label\" style=\"margin-right: 0;\">成绩值</span> <span class=\"number\">" +
                                     Utility.Round(course.CourseInfo.Mark) + "</span></div></div> </li>");
                                 suffix.Append(
                                     "<div class=\"line_light\"><div class=\"light_left\"></div><div class=\"light_right\"></div></div>");
                                 */
                            prefix.Append("<li style=\"margin-right: 0;\"><a href=\"" + "course.aspx?id=" + course.ID +
              "\" class=\"course\" title=" + course.CourseName + ">" + Utility.ShortenClassName(course.CourseName) + "</a>");
                            prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.Teacher.ID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                          Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                            prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.RcmdNum + "</span>推荐</p>");
                            suffix.Append(
                                "<div class=\"bar_list\"><span class=\"bar_label\">含金量</span><div class=\"bar\"><div class=\"bar1_head\"></div><div class=\"bar1_body\" style=\"width:");
                            suffix.Append(course.CourseInfo.Gold * 14.8);
                            suffix.Append("px;\"> </div><div class=\"bar1_foot\"><span>" + Utility.Round(course.CourseInfo.Gold) +
                                          "</span></div></div></div>");
                            suffix.Append(
                                "<div class=\"bar_list\"><span class=\"bar_label\">轻松度</span><div class=\"bar\"><div class=\"bar2_head\"></div><div class=\"bar2_body\" style=\"width:");
                            suffix.Append(course.CourseInfo.Relax * 14.8);
                            suffix.Append("px;\"></div><div class=\"bar2_foot\"><span>" + Utility.Round(course.CourseInfo.Relax));
                            suffix.Append(
                                "</span></div></div></div><div class=\"bar_list\"><span class=\"bar_label\">成绩值</span><div class=\"bar\"><div class=\"bar3_head\"></div><div class=\"bar3_body\" style=\"width:");
                            suffix.Append(course.CourseInfo.Mark * 14.8);
                            suffix.Append("px;\"></div><div class=\"bar3_foot\"><span>" + Utility.Round(course.CourseInfo.Mark) +
                                          "</span></div></div></div></li>");

                        }
                        else
                        {
                            prefix.Append("<li><a href=\"" + "course.aspx?id=" + course.ID +
              "\" class=\"course\" title=" + course.CourseName + ">" + Utility.ShortenClassName(course.CourseName) + "</a>");
                            prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.Teacher.ID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                           Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                            prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.RcmdNum + "</span>推荐</p>");
                            suffix.Append(
                                "<div class=\"bar_list\"><span class=\"bar_label\">含金量</span><div class=\"bar\"><div class=\"bar1_head\"></div><div class=\"bar1_body\" style=\"width:");

                            suffix.Append(course.CourseInfo.Gold * 14.8);
                            suffix.Append("px;\"> </div><div class=\"bar1_foot\"><span>" + Utility.Round(course.CourseInfo.Gold) +
                                          "</span></div></div></div>");
                            suffix.Append(
                                "<div class=\"bar_list\"><span class=\"bar_label\">轻松度</span><div class=\"bar\"><div class=\"bar2_head\"></div><div class=\"bar2_body\" style=\"width:");
                            suffix.Append(course.CourseInfo.Relax * 14.8);
                            suffix.Append("px;\"></div><div class=\"bar2_foot\"><span>" + Utility.Round(course.CourseInfo.Relax));
                            suffix.Append(
                                "</span></div></div></div><div class=\"bar_list\"><span class=\"bar_label\">成绩值</span><div class=\"bar\"><div class=\"bar3_head\"></div><div class=\"bar3_body\" style=\"width:");
                            suffix.Append(course.CourseInfo.Mark * 14.8);
                            suffix.Append("px;\"></div><div class=\"bar3_foot\"><span>" + Utility.Round(course.CourseInfo.Mark) +
                                          "</span></div></div></div></li>");
                            /*
                            prefix.Append("<li><a href=\"" + "course.aspx?id=" + course.ID + "\" class=\"course\" title=" + course.CourseName + ">" +
                                      Utility.ShortenClassName(course.CourseName)+"</a>");
                            prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.TeacherID+ "\" class=\"teacher\" title="+Utility.FullName(course.Teacher.TeacherName)+">" +
                                          Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                            prefix .Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");

                            suffix.Append(
                                "<div class=\"bar_list\"><div class=\"plain_text\"><span class=\"bar_label\">含金量</span> <span class=\"number\">" +
                                Utility.Round(course.CourseInfo.Gold) + "</span></div>");
                            suffix.Append(
                                "<div class=\"plain_text\"><span class=\"bar_label\">轻松度</span> <span class=\"number\">" +
                                Utility.Round(course.CourseInfo.Relax) + "</span></div>");
                            suffix.Append(
                                "<div class=\"plain_text\"><span class=\"bar_label\" style=\"margin-right: 0;\">成绩值</span> <span class=\"number\">" +
                                Utility.Round(course.CourseInfo.Mark) + "</span></div></div> </li>");*/
                        }
                    }
                    else
                    {
                       if(index%3==2)
                    {
                   /*     prefix.Append("<li style=\"margin-right: 0;\"><a href=\"" + "course.aspx?id=" + course.ID + "\" class=\"course\" title=" + course.CourseName + ">" +
                                  Utility.ShortenClassName(course.CourseName) + "</a>");
                        prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.TeacherID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                  Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                        prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");

                        suffix.Append(
                            "<div class=\"bar_list\"><div class=\"plain_text\"><span class=\"bar_label\">含金量</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Gold) + "</span></div>");
                        suffix.Append(
                            "<div class=\"plain_text\"><span class=\"bar_label\">轻松度</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Relax) + "</span></div>");
                        suffix.Append(
                            "<div class=\"plain_text\"><span class=\"bar_label\" style=\"margin-right: 0;\">成绩值</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Mark) + "</span></div></div> </li>");
                        suffix.Append(
                            "<div class=\"line_light\"><div class=\"light_left\"></div><div class=\"light_right\"></div></div>");
                        */
                        prefix.Append("<li style=\"margin-right: 0;\"><a href=\"" + "course.aspx?id=" + course.ID +
          "\" class=\"course\" title=" + course.CourseName + ">" + Utility.ShortenClassName(course.CourseName) + "</a>");
                        prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.Teacher.ID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                      Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                        prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");
                        suffix.Append(
                            "<div class=\"bar_list\"><span class=\"bar_label\">含金量</span><div class=\"bar\"><div class=\"bar1_head\"></div><div class=\"bar1_body\" style=\"width:");
                        suffix.Append(course.CourseInfo.Gold * 14.8);
                        suffix.Append("px;\"> </div><div class=\"bar1_foot\"><span>" + Utility.Round(course.CourseInfo.Gold) +
                                      "</span></div></div></div>");
                        suffix.Append(
                            "<div class=\"bar_list\"><span class=\"bar_label\">轻松度</span><div class=\"bar\"><div class=\"bar2_head\"></div><div class=\"bar2_body\" style=\"width:");
                        suffix.Append(course.CourseInfo.Relax * 14.8);
                        suffix.Append("px;\"></div><div class=\"bar2_foot\"><span>" + Utility.Round(course.CourseInfo.Relax));
                        suffix.Append(
                            "</span></div></div></div><div class=\"bar_list\"><span class=\"bar_label\">成绩值</span><div class=\"bar\"><div class=\"bar3_head\"></div><div class=\"bar3_body\" style=\"width:");
                        suffix.Append(course.CourseInfo.Mark * 14.8);
                        suffix.Append("px;\"></div><div class=\"bar3_foot\"><span>" + Utility.Round(course.CourseInfo.Mark) +
                                      "</span></div></div></div></li>");
                    
                    }
                    else
                    {
                        prefix.Append("<li><a href=\"" + "course.aspx?id=" + course.ID +
          "\" class=\"course\" title=" + course.CourseName + ">" + Utility.ShortenClassName(course.CourseName) + "</a>");
                        prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.Teacher.ID + "\" class=\"teacher\" title=" + Utility.FullName(course.Teacher.TeacherName) + ">" +
                                       Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                        prefix.Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");
                        suffix.Append(
                            "<div class=\"bar_list\"><span class=\"bar_label\">含金量</span><div class=\"bar\"><div class=\"bar1_head\"></div><div class=\"bar1_body\" style=\"width:");

                        suffix.Append(course.CourseInfo.Gold * 14.8);
                        suffix.Append("px;\"> </div><div class=\"bar1_foot\"><span>" + Utility.Round(course.CourseInfo.Gold) +
                                      "</span></div></div></div>");
                        suffix.Append(
                            "<div class=\"bar_list\"><span class=\"bar_label\">轻松度</span><div class=\"bar\"><div class=\"bar2_head\"></div><div class=\"bar2_body\" style=\"width:");
                        suffix.Append(course.CourseInfo.Relax * 14.8);
                        suffix.Append("px;\"></div><div class=\"bar2_foot\"><span>" + Utility.Round(course.CourseInfo.Relax));
                        suffix.Append(
                            "</span></div></div></div><div class=\"bar_list\"><span class=\"bar_label\">成绩值</span><div class=\"bar\"><div class=\"bar3_head\"></div><div class=\"bar3_body\" style=\"width:");
                        suffix.Append(course.CourseInfo.Mark * 14.8);
                        suffix.Append("px;\"></div><div class=\"bar3_foot\"><span>" + Utility.Round(course.CourseInfo.Mark) +
                                      "</span></div></div></div></li>");
                        /*
                        prefix.Append("<li><a href=\"" + "course.aspx?id=" + course.ID + "\" class=\"course\" title=" + course.CourseName + ">" +
                                  Utility.ShortenClassName(course.CourseName)+"</a>");
                        prefix.Append("<a href=\"" + "teacher.aspx?id=" + course.TeacherID+ "\" class=\"teacher\" title="+Utility.FullName(course.Teacher.TeacherName)+">" +
                                      Utility.ShortenTeacherName(course.Teacher.TeacherName) + "</a>");
                        prefix .Append("<p class=\"browse\"><span>" + course.CourseInfo.Visits + "</span>浏览</p>");

                        suffix.Append(
                            "<div class=\"bar_list\"><div class=\"plain_text\"><span class=\"bar_label\">含金量</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Gold) + "</span></div>");
                        suffix.Append(
                            "<div class=\"plain_text\"><span class=\"bar_label\">轻松度</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Relax) + "</span></div>");
                        suffix.Append(
                            "<div class=\"plain_text\"><span class=\"bar_label\" style=\"margin-right: 0;\">成绩值</span> <span class=\"number\">" +
                            Utility.Round(course.CourseInfo.Mark) + "</span></div></div> </li>");*/
                    }  
                    }
                   
                    LinkButton button = new LinkButton();
                    button.CssClass = "ask_place";    
                    button.ID = course.ID.ToString();
                    button.Click+=PlaceClick;
                    var label = (Label) e.Item.FindControl("label_content");
                    label.Controls.Add(new Literal{Text = prefix.ToString()});
                    if(course.CoursePlaces.Count() == 0)label.Controls.Add(button);
                    label.Controls.Add(new Literal {Text = suffix.ToString()});
                break;
            }
        }
        protected void PageDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            int page = e.Item.ItemIndex + 1;
            if (page != int.Parse(current_page.Value))
            {
                var linkButton = new LinkButton
                {
                    ID = "page" + page,
                    Text = string.Format("<li>{0}</li>", page)
                };
                linkButton.Click += PageClick;
                ToolkitScriptManager1.RegisterAsyncPostBackControl(linkButton);
                e.Item.FindControl("Label_Page").Controls.Add(linkButton);
            }
            else
                e.Item.FindControl("Label_Page").Controls.Add(new Literal { Text = string.Format("<li>{0}</li>", page) });
 
        }
    }


}